/*
 * DppmParams.cpp
 *
 *  Created on: 20/01/2014
 *      Author: root
 */

#include <stdlib.h>
//#include <mpi.h>
#include <iostream>

using namespace std;

#include "DppmParams.h"


DppmParams::DppmParams(int numAct, int T, int* numPreds, int** Preds,
		int* numModes, int** ModesCosts, int** ModesTimes) {

	this->numAct = numAct;
	this->deadline = T;
	this->acts = new Activity[numAct];

	for (int i = 0; i < numAct; i++) {

		if (Preds[i] == NULL) {
			this->acts[i].numPreds = 0;
			this->acts[i].preds = NULL;
		} else {
			this->acts[i].numPreds = numPreds[i];
			this->acts[i].preds = new int[this->acts[i].numPreds];

			for (int j = 0; j < this->acts[i].numPreds; j++)
				this->acts[i].preds[j] = Preds[i][j];
		}

		if (ModesCosts[i] == NULL) {
			this->acts[i].numModes = 0;
			this->acts[i].costs = NULL;
			this->acts[i].times = NULL;
		} else {
			this->acts[i].numModes = numModes[i];
			this->acts[i].costs = new int[this->acts[i].numModes];
			this->acts[i].times = new int[this->acts[i].numModes];

			for (int j = 0; j < this->acts[i].numModes; j++) {
				this->acts[i].costs[j] = ModesCosts[i][j];
				this->acts[i].times[j] = ModesTimes[i][j];
			}
		}
	}
}

int DppmParams::getDeadline(){
	return this->deadline;
}


int DppmParams::getNumActivities(){
	return this->numAct;
}

int DppmParams::getActivityNumModes(int act){
	return this->acts[act].numModes;
}

int* DppmParams::getActivityModesCosts(int act){
	return this->acts[act].costs;
}

int* DppmParams::getActivityModesTimes(int act){
	return this->acts[act].times;
}

int DppmParams::getActivityNumPreds(int act){
	return this->acts[act].numPreds;
}

int* DppmParams::getActivityPreds(int act){
	return this->acts[act].preds;
}

int getMaxTime(DppmParams * dppm, Tuple* t, int act) {

	int max = 0;
	int predInd;
	int timePred;
	int * preds = NULL;

	for (int i = 0; i < dppm->getActivityNumPreds(act); i++) {
		//obtengo el indice del predecesor
		preds = dppm->getActivityPreds(act);
		predInd = preds[i];
		//obtengo el tiempo del predecesor
		timePred = t->times(predInd);
		max = (max < timePred) ? timePred : max;
	}
	return max;
}


int DppmParams::getPredsMaxTime(Tuple* t){
	//precondicion, todos los predecesores tienen un modo asigando en la Tupla t
	int ret = getMaxTime(this, t, t->lastAct());
//	cout<<"tiempo de la actividad : "<< t->now <<"  ret: "<< ret <<endl;
	return ret;
}

void DppmParams::Print() {

	cout << "#############  DPPM PARAMS ###################" << endl;
	cout << "numAct: " << this->numAct << endl;
	cout << "deadLine: " << this->deadline << endl;
	for (int i = 0; i < this->numAct; i++) {
		cout << "\t" << "numModes: " << this->acts[i].numModes << endl;
		cout << "\t" << "numPreds: " << this->acts[i].numPreds << endl;
		cout << "\t" << "Modes: (cost/time)" << endl;
		for (int j = 0; j < this->acts[i].numModes; j++) {
			cout << "\t\t" << "(" << this->acts[i].costs[j] << "/"
					<< this->acts[i].times[j] << ")" << endl;
		}
		cout << "\t" << "Preds: ";
		for (int j = 0; j < this->acts[i].numPreds; j++) {
			cout << "| " << this->acts[i].preds[j] << " ";
		}
		cout << endl;
	}
	cout << "#############  DPPM PARAMS ###################" << endl;

}

DppmParams::~DppmParams() {

	for (int i = 0; i < this->numAct; i++) {
		delete[] this->acts[i].preds;
		delete[] this->acts[i].costs;
		delete[] this->acts[i].times;
	}
	delete[] this->acts;

}

